package com.fashion.service;

import com.fashion.dao.CollectedNewsDao;
import com.fashion.model.CollectedNews;
import com.fashion.utils.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: gnyk
 * Date: 13-2-17
 */
@Service("collectedNewsService")
public class CollectedNewsServiceImpl implements CollectedNewsService {
    private static final Logger logger = Logger.getLogger(ColumnServiceImpl.class);

    private CollectedNewsDao collectedNewsDao;
    @Resource(name = "hibernateTemplate")
    private HibernateTemplate template;

    @Override
    public void saveOrUpdate(Object object) {
        collectedNewsDao.saveOrUpdate(object);
    }

    @Override
    public void deleteEntityById(String id) {
        collectedNewsDao.deleteById(id, CollectedNews.class);
    }

    @Override
    public void deleteEntity(Object object) {
        collectedNewsDao.delete(object);
    }

    @Resource(name = "collectedNewsDao")
    public void setCollectedNewsDao(CollectedNewsDao collectedNewsDao) {
        this.collectedNewsDao = collectedNewsDao;
    }

    @Override
    public List<CollectedNews> searchCollectedNews(String collectedNewsTitle, int page, int rows) {
        StringBuilder hql = new StringBuilder("select new CollectedNews(ns.id, ns.title, ns.introduction, ns.source,ns.sourceLink, ns.createDate)  from CollectedNews ns ");
        Map<String, Object> parameters = new HashMap<String, Object>();

        if (!StringUtils.isNullOrEmpty(collectedNewsTitle)) {
            hql.append(" where ns.title like :title");
            parameters.put("title", collectedNewsTitle + "%");
        }
        hql.append(" order by ns.createDate desc");

        return collectedNewsDao.findItemsWithPage(hql.toString(), parameters, page, rows);
    }

    @Override
    public int countCollectedNews(String collectedNewsTitle) {
        StringBuilder hql = new StringBuilder("select count(ns.id) from CollectedNews ns ");
        Map<String, Object> parameters = new HashMap<String, Object>();
        if (!StringUtils.isNullOrEmpty(collectedNewsTitle)) {
            hql.append(" where ns.title like :title");
            parameters.put("title", collectedNewsTitle + "%");
        }

        return collectedNewsDao.countItems(hql.toString(), parameters);
    }

    @Override
    public CollectedNews getCollectedNewsById(String id) {
        return collectedNewsDao.getEntityById(CollectedNews.class,id);
    }

    @Override
    public void deleteAllCollectedNews() {
        collectedNewsDao.bulkDelete("delete CollectedNews");
    }

    @Override
    public List<CollectedNews> findAllCollectedNewsWithTitle() {
        String hql = "select new CollectedNews(ns.id, ns.title)  from CollectedNews ns ";

        return collectedNewsDao.find(hql);
    }
}
